//
//  ReadConfig.m
//  QiYReader
//
//  Created by 奇阅科技 on 14/12/5.
//  Copyright (c) 2014年 奇阅科技. All rights reserved.
//

#import "ReadConfig.h"
#import "PathHelper.h"
@implementation ReadConfig
@synthesize bgColor = _bgColor;
@synthesize titleColor = _titleColor;
@synthesize textColor = _textColor;
static ReadConfig *readConfig;
+ (ReadConfig *)currentAccount {
    @synchronized(self) {
        if (!readConfig) {
            [self loadAccount];
        }
    }
    return readConfig;
}
+ (void)loadAccount {
    readConfig = [NSKeyedUnarchiver unarchiveObjectWithFile:[self getAccountsStoragePath]];
    if (!readConfig) {
        readConfig = [ReadConfig new];
        readConfig.foutSize = 18;
        readConfig.pointsPerSecond = 30;
    }
}
+ (NSString *)getAccountsStoragePath {
    return [[PathHelper documentDirectoryPathWithName:@"db"]
            stringByAppendingPathComponent:@"ReadConfig"];
}
+ (void)saveAccount:(ReadConfig *)acc {
    [NSKeyedArchiver archiveRootObject:acc toFile:[self getAccountsStoragePath]];
    readConfig = acc;
}

- (id)initWithCoder:(NSCoder *)decoder {
    self = [super init];
    if (self != nil) {
        _foutSize = [decoder decodeFloatForKey:@"foutSize"];
        _isBlack = [decoder decodeBoolForKey:@"isBlack"];
        _colorConfig = [decoder decodeIntForKey:@"colorConfig"];
        _transitionStyle = [decoder decodeIntForKey:@"transitionStyle"];
        _orientationConfig = [decoder decodeIntForKey:@"orientationConfig"];
        _pointsPerSecond = [decoder decodeFloatForKey:@"pointsPerSecond"];
//        _bgColor = [decoder decodeObjectForKey:@"bgColor"];
//        _titleColor = [decoder decodeObjectForKey:@"titleColor"];
//        _textColor= [decoder decodeObjectForKey:@"textColor"];
        
    }
    return self;
}

- (void)encodeWithCoder:(NSCoder *)encoder {
    [encoder encodeFloat:self.foutSize forKey:@"foutSize"];
    [encoder encodeBool:self.isBlack forKey:@"isBlack"];
    [encoder encodeInt:self.colorConfig forKey:@"colorConfig"];
    [encoder encodeInt:self.transitionStyle forKey:@"transitionStyle"];
    [encoder encodeInt:self.orientationConfig forKey:@"orientationConfig"];
    [encoder encodeFloat:self.pointsPerSecond forKey:@"pointsPerSecond"];
//    [encoder encodeObject:self.bgColor forKey:@"bgColor"];
//    [encoder encodeObject:self.titleColor forKey:@"titleColor"];
//    [encoder encodeObject:self.textColor forKey:@"textColor"];
}
-(void)setFoutSize:(float)foutSize{
    if (foutSize<12||foutSize>28) {
        return;
    }
    _foutSize = foutSize;
    [ReadConfig saveAccount:self];
}
-(void)setColorConfig:(ColorConfig)colorConfig{
    _colorConfig = colorConfig;
    [ReadConfig saveAccount:self];
}
-(void)setIsBlack:(BOOL)isBlack{
    _isBlack = isBlack;
    [ReadConfig saveAccount:self];
}
-(void)setPointsPerSecond:(CGFloat)pointsPerSecond{
    _pointsPerSecond = pointsPerSecond;
    [ReadConfig saveAccount:self];
}
-(void)setTransitionStyle:(TransitionStyle)transitionStyle{
    _transitionStyle = transitionStyle;
    [ReadConfig saveAccount:self];
}
-(void)setOrientationConfig:(OrientationConfig)orientationConfig{
    _orientationConfig = orientationConfig;
    [ReadConfig saveAccount:self];
}
-(UIColor *)bgColor{
    if (self.isBlack) {
        return [UIColor colorWithRed:(22)/255.0 green:(22)/255.0 blue:(22)/255.0 alpha:1];
    }else if (self.colorConfig == ColorConfig0) {
        return [UIColor colorWithRed:(246)/255.0 green:(245)/255.0 blue:(241)/255.0 alpha:1];
    }else if (self.colorConfig == ColorConfig1){
        return [UIColor colorWithRed:(240)/255.0 green:(226)/255.0 blue:(206)/255.0 alpha:1];
    }else if (self.colorConfig == ColorConfig2){
        if ([UIScreen mainScreen].bounds.size.height<500) {
            return [UIColor colorWithPatternImage:[UIImage imageNamed:@"readConfigBG4"]];
        } else if ([UIScreen mainScreen].bounds.size.height<600) {
            return [UIColor colorWithPatternImage:[UIImage imageNamed:@"readConfigBG5"]];
        } else if ([UIScreen mainScreen].bounds.size.height<700) {
            return [UIColor colorWithPatternImage:[UIImage imageNamed:@"readConfigBG6"]];
        } else{
            return [UIColor colorWithPatternImage:[UIImage imageNamed:@"readConfigBG6+"]];
        }
    }else if (self.colorConfig == ColorConfig3){
        return [UIColor colorWithRed:(199)/255.0 green:(223)/255.0 blue:(201)/255.0 alpha:1];
    }else if (self.colorConfig == ColorConfig4){
        return [UIColor colorWithRed:(203)/255.0 green:(222)/255.0 blue:(238)/255.0 alpha:1];
    }else if (self.colorConfig == ColorConfig5){
        return [UIColor colorWithRed:(241)/255.0 green:(107)/255.0 blue:(159)/255.0 alpha:1];
    }
    return [UIColor colorWithRed:(246)/255.0 green:(245)/255.0 blue:(241)/255.0 alpha:1];
}
-(UIColor *)titleColor{
    if (self.isBlack) {
        return [UIColor colorWithRed:(65)/255.0 green:(65)/255.0 blue:(65)/255.0 alpha:1];
    }else if (self.colorConfig == ColorConfig2){
        return [UIColor colorWithRed:(122)/255.0 green:(113)/255.0 blue:(97)/255.0 alpha:1];
    }else if (self.colorConfig == ColorConfig5){
        return [UIColor colorWithRed:(252)/255.0 green:(214)/255.0 blue:(229)/255.0 alpha:1];
    }
    return [UIColor colorWithRed:(140)/255.0 green:(140)/255.0 blue:(140)/255.0 alpha:1];
}
-(UIColor *)textColor{
    if (self.isBlack) {
        return [UIColor colorWithRed:(83)/255.0 green:(83)/255.0 blue:(83)/255.0 alpha:1];
    }else if (self.colorConfig == ColorConfig5){
        return [UIColor whiteColor];
    }
    return [UIColor colorWithRed:(50)/255.0 green:(50)/255.0 blue:(50)/255.0 alpha:1];
}
@end
